package net.lab1024.sa.admin.module.business.tz.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.lab1024.sa.admin.module.business.place.dao.PlaceDao;
import net.lab1024.sa.admin.module.business.place.dao.RSchoolDao;
import net.lab1024.sa.admin.module.business.place.domain.entity.PlaceEntity;
import net.lab1024.sa.admin.module.business.place.domain.form.PlaceQueryForm;
import net.lab1024.sa.admin.module.business.place.domain.vo.PlaceVO;
import net.lab1024.sa.admin.module.business.place.service.PlaceService;
import net.lab1024.sa.admin.module.business.place.service.RSchoolService;
import net.lab1024.sa.admin.module.business.tz.dao.TzDao;
import net.lab1024.sa.admin.module.business.tz.domain.entity.TzEntity;
import net.lab1024.sa.admin.module.business.tz.domain.form.TzAddForm;
import net.lab1024.sa.admin.module.business.tz.domain.form.TzQueryForm;
import net.lab1024.sa.admin.module.business.tz.domain.form.TzUpdateForm;
import net.lab1024.sa.admin.module.business.tz.domain.vo.TzVO;
import net.lab1024.sa.admin.module.system.type.domain.TypeEntity;
import net.lab1024.sa.admin.module.system.type.service.TypeService;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 通知 Service
 *
 * @Author syf
 * @Date 2025-06-03 17:53:30
 * @Copyright 畅想科技
 */

@Service
public class TzService {

    @Resource
    private TzDao tzDao;
    @Resource
    private TypeService typeService;
    @Resource
    private RSchoolDao rSchoolDao;
    @Resource
    private PlaceDao placeDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<TzVO> queryPage(TzQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<TzVO> list = tzDao.queryPage(page, queryForm);
        List<TypeEntity> objectList = typeService.getTypeList("notice_obj").getData();
        List<PlaceEntity> placeList = rSchoolDao.query();

        List<String> objectLists = objectList.stream()
                .map(TypeEntity::getTypeName)
                .collect(Collectors.toList());
        for (TzVO tzVO : list) {
            tzVO.setNoticeToName(objectLists.get(Math.toIntExact(tzVO.getNoticeTo())));
            for (PlaceEntity place : placeList) {
                if (place.getPlaceId().equals(tzVO.getNoticeRange())) {
                    tzVO.setNoticeRangeName(place.getPlaceName());
                    break;
                }
        }
        }
        PageResult<TzVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }


    /**
     * 添加
     */
    public ResponseDTO<String> add(TzAddForm addForm) {
        TzEntity tzEntity = SmartBeanUtil.copy(addForm, TzEntity.class);
        tzEntity.setCreateTime(LocalDateTime.now());
        tzDao.insert(tzEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(TzUpdateForm updateForm) {
        TzEntity tzEntity = SmartBeanUtil.copy(updateForm, TzEntity.class);
        tzDao.updateById(tzEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        tzDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        tzDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<TzVO> view(Long noticeId) {
        TzEntity tzEntity = tzDao.selectById(noticeId);
        TzVO tzVO = SmartBeanUtil.copy(tzEntity, TzVO.class);
         PlaceEntity placeEntity = placeDao.selectById(tzVO.getNoticeRange());
         tzVO.setNoticeRangeName(placeEntity.getPlaceName());
        List<TypeEntity> objectList = typeService.getTypeList("notice_obj").getData();
        tzVO.setNoticeToName (objectList.get(Math.toIntExact(tzVO.getNoticeTo())).getTypeName());
        return ResponseDTO.ok(tzVO);
    }
}
